package tt.dz.order.pojo;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.dao.SysApplicationContextAware;
import tt.dz.order.service.OperationManagementService;
import tt.dz.order.vo.MerchantVO;
import tt.dz.order.vo.OrderPayVo;

public class OrderAlreadyPaid {
    // "merchantCode" varchar(255) COLLATE "default",
    // "price" numeric(10,2),
    // "alreadyPaid" numeric(10,2)
    // 运营商号
    private String merchantCode;
    private String merchantCodeName;
    // 总金额
    private BigDecimal price;
    // 已付金额
    private BigDecimal alreadyPaid;
    //本次支付金额
    private BigDecimal payPrice;
    private Date time;
    public String getMerchantCode() {
        return merchantCode;
    }

    public void setMerchantCode(String merchantCode) {
        this.merchantCode = merchantCode;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public BigDecimal getAlreadyPaid() {
        return alreadyPaid;
    }

    public void setAlreadyPaid(BigDecimal alreadyPaid) {
        this.alreadyPaid = alreadyPaid;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public BigDecimal getPayPrice() {
        return payPrice;
    }

    public void setPayPrice(BigDecimal payPrice) {
        this.payPrice = payPrice;
    }

    public String getMerchantCodeName() {
        return merchantCodeName;
    }

    public void setMerchantCodeName(String merchantCodeName) {
        this.merchantCodeName = merchantCodeName;
    }

    public void save() {
        String sql = "insert into order_already_paid(merchantCode, price, alreadyPaid,time) values(:merchantCode, :price, :alreadyPaid, :time)";
        BaseDAOUtil.updateObject(sql, this);
    }

    public void updatePrice() {
        String sql = "update order_already_paid set price=:price where merchantCode = :merchantCode and time=:time";
        BaseDAOUtil.updateObject(sql, this);
    }

    public void updateAlreadyPaid() {
        String sql = "update order_already_paid set alreadyPaid=:alreadyPaid where merchantCode = :merchantCode and time=:time";
        BaseDAOUtil.updateObject(sql, this);
    }

    public OrderAlreadyPaid selectByMerchantCode(String merchantCode) {
        String sql = "SELECT a.merchantCode, price, alreadyPaid,time ,b.NAME as merchantCodeName FROM order_already_paid a left join ms_merchant b on a.merchantcode =b.merchant_code WHERE a.merchantCode =:merchantCode";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("merchantCode", merchantCode);
        return BaseDAOUtil.queryForObject(sql, paramMap, OrderAlreadyPaid.class);
    }
    public OrderAlreadyPaid selectByMerchantCodeAndTime(String merchantCode,Date time) {
        String sql = "SELECT a.merchantCode, price, alreadyPaid,time ,b.NAME as merchantCodeName FROM order_already_paid a left join ms_merchant b on a.merchantcode =b.merchant_code WHERE a.merchantCode =:merchantCode and a.time=:time";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("merchantCode", merchantCode);
        paramMap.put("time", time);
        return BaseDAOUtil.queryForObject(sql, paramMap, OrderAlreadyPaid.class);
    }
    public OrderAlreadyPaid selectinfoByMerchantCode(String merchantCode) {
        String sql = "select a.*, b.NAME as merchantCodeName ,pay.alreadyPaid from ( SELECT merchantCode, sum(price) as price  FROM order_already_paid  group by merchantCode) a left join ms_merchant b on a.merchantcode =b.merchant_code LEFT JOIN ( SELECT merchantcode, SUM (caculatedtotalprice) AS alreadyPaid FROM order_pay_info where status='7'  GROUP BY merchantcode) pay on pay.merchantcode=A.merchantcode where  a.merchantCode =:merchantCode";
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("merchantCode", merchantCode);
        return BaseDAOUtil.queryForObject(sql, paramMap, OrderAlreadyPaid.class);
    }

    /**
     * 每天按照运营商汇总当日金额定时任务
     */
    public static void statistics() {
        try {
            Calendar nowCalendar = Calendar.getInstance();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startDatestr = dateFormat.format(nowCalendar.getTime());
             nowCalendar.setTime(dateFormat.parse(startDatestr));
            Date  startDate =nowCalendar.getTime();
            nowCalendar.add(Calendar.DAY_OF_MONTH,1 );
            String  endDateStr = dateFormat.format(nowCalendar.getTime());
            Date endDate =  nowCalendar.getTime();
            String sql = "SELECT sum(realtotalprice) as realtotalprice,serviceprovider,serviceprovider FROM order_item WHERE  createtime <:endDate and  createtime >:startDate GROUP BY serviceprovider";
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startDate", startDate);
            paramMap.put("endDate", endDate);
            List<Map<String, Object>> mapList = BaseDAOUtil.queryForList(sql, paramMap);
            for (Map<String, Object> map : mapList) {
                // OrderAppConfig.GET().getMerchantCode().equals(map.get("realtotalprice"));
                OrderAlreadyPaid orderAlreadyPaid = new OrderAlreadyPaid();
                orderAlreadyPaid = orderAlreadyPaid.selectByMerchantCodeAndTime(map.get("serviceprovider").toString(),startDate);
                if (orderAlreadyPaid == null) {
                    orderAlreadyPaid = new OrderAlreadyPaid();
                    orderAlreadyPaid.setMerchantCode(map.get("serviceprovider").toString());
                    orderAlreadyPaid.setPrice(new BigDecimal(map.get("realtotalprice").toString()));
                    orderAlreadyPaid.setAlreadyPaid(BigDecimal.ZERO);
                    orderAlreadyPaid.setTime(new Date());
                    orderAlreadyPaid.save();
                } else {
//                    orderAlreadyPaid.setMerchantCode(map.get("serviceprovider").toString());
                    orderAlreadyPaid.setPrice(new BigDecimal(map.get("realtotalprice").toString()));
//                    orderAlreadyPaid.setAlreadyPaid(BigDecimal.ZERO);
//                    orderAlreadyPaid.setTime(new Date());
                    orderAlreadyPaid.updatePrice();
                }
                MerchantVO merchant=new MerchantVO();
                merchant=  merchant.queryMerchantByCode(map.get("serviceprovider").toString());
                if(merchant.getTransmatic()==1){
                    orderAlreadyPaid= orderAlreadyPaid.selectinfoByMerchantCode(map.get("serviceprovider").toString());
                    OperationManagementService operationManagementService = SysApplicationContextAware.getApplication().getBean("operationManagementService", OperationManagementService.class);
                    if(orderAlreadyPaid.getPrice().doubleValue()<=orderAlreadyPaid.getAlreadyPaid().doubleValue()){
                        continue;
                    }
                    orderAlreadyPaid.setPayPrice(orderAlreadyPaid.getPrice().subtract(orderAlreadyPaid.getAlreadyPaid()));
                    try {
                        operationManagementService.pay(orderAlreadyPaid,null ,true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // BaseDAOUtil.updateObject(sql, this);
    }

}
